﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rem.Objects;
using System.IO;

namespace FileDB
{
    public partial class Customer
    {
        public Customer()
        {
            _id = QueryConstants.UnsavedObjectId;
            _name = "名称";
        }
        private int _id;

        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }
        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public void Save()
        {
            if (this.Id > 0)
                Save(this, GetFileName(this.Id));
            else
                Insert(this);
        }

        public object Clone()
        {
            Customer employee = new Customer()
            {
                Id = QueryConstants.UnsavedObjectId,
                Name = string.Format(QueryConstants.CloneFomateTitleTemplate, BaseObjectTypes.Customer, this.Id, this.Name),
                CardId = this.CardId
            };
            return employee;
        }

        public bool Delete()
        {
            if (this._id > 0)
            {
                if (this._id == Customer.GetSelfCompany().Id)
                {
                    return false;
                }
                var count = (from c in Contract.GetAllContract()
                             where c.PartAId == this._id || c.PartBId == this._id
                             select c.Id).Count();
                if (count == 0)
                {
                    File.Delete(GetFileName(this));
                    return true;
                }
            }
            return false;
        }
        public string CardId { get; set; }

        public string Phone { get; set; }

        public string Comment { get; set; }


        public static Customer GetCustomer(int id)
        {
            return Customer.GetData(GetFileName(id));
        }

    }
}
